<?php

class Event extends AppModel {

    var $name = 'Event';
    //The Associations below have been created with all possible keys, those that are not needed can be removed
    var $hasMany = array(
        'EventHost' => array(
            'className' => 'EventHost',
            'foreignKey' => 'event_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Invite' => array(
            'className' => 'Invite',
            'foreignKey' => 'event_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'Rsvp' => array(
            'className' => 'Rsvp',
            'foreignKey' => 'event_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    var $validate = array(



        'capacity' => array(
           'rule' => array('comparison', '>=', 2),
           'message' => 'capacity must be greater than one'
        ),

        'event_start' => array
            (
            
            'event_startRule-1' => array
                (
                'rule' => 'notEmpty',
                'message' => 'You can not leave this field blank!'
            ),

              'event_startRule-2' => array
                (
                'rule' =>array('todayDate'),
                'message' => 'Event can not start before today!'
           )


        ),

        'event_end' => array
            (
            'event_endRule-1' => array
                (
                'rule' => array('compareDates'),
                'message' => 'Event can not end before it starts!'
            )


        ),
        
        'rsvp_start' => array
            (
            
            'rsvp_startRule-1' => array
                (
                'rule' => array('todayDate'),
                'message' => 'RSVPing can not start before today!'
            ),

              'event_startRule-2' => array
                (
                'rule' =>array('RSVPDate'),
                'message' => 'RSVPing can not start after the event does!'
           )


        ),
      
        
        
         

        'event_name' => array(
            'rule' => 'notEmpty'
        ),
        'description' => array(
            'rule' => 'notEmpty'
        ),
        'start_time' => array(
            'rule' => 'notEmpty'
        ),
        'end_time' => array(
            'rule' => 'notEmpty'
        ),

        
         'rsvp_end' => array
            (

                'rule' => array('RSVPcompareDates'),
                'message' => 'Event can not end before it starts!'




        ),
        
        'event_type' => array(
        'rule' => array('notEmpty'),
        'message' =>'You must elect public or invite!'


        ),




            'venue_addr1' => array(
            'rule' => 'notEmpty'
        ),
        'venue_city' => array(
            'rule' => 'notEmpty'
        ),
        'venue_state' => array(
            'rule' => 'notEmpty'
        ),
        'venue_postal' => array(

           'venue_postalRule-1'=> array(
            'rule' => 'numeric',
            'message' => "The postal code must be numbers only!"
            ),

            'venue_postalRule-2' => array(
                'rule' => array('minLength', 5),
                'message' => "The postal code must can not have less than five numbers in it!"
            ),

            'venue_postalRule-3'=> array(
                'rule' => array('maxLength', 5),
                'message' => "The postal code can not have more than five numbers in it!"
           )
       )

        
    );


    function TodayDate($start)

    {

        $date = implode(' ',$start);
        $dtz= new DateTimeZone("UTC");
        $bigDate= new DateTime($date, $dtz);
         $d1 = new DateTime("today");
         $s=$d1->format('U');
        $s2= $bigDate->format('U');
         if ($s< $s2)
      {

            return true;

             }

        
    }
    /* This function is designed to take a date in array format from the
     * values that have been entered in in the add.ctp or edit.ctp page, and then
     * check create a new date time object from tis values that can be properly formated into
     * the seconds only format.  After that it is checked agaisnt today's current date tim
     * also turned into the seconds only format to make sure that the value passed to the
     * function is greater than today's date, thus allowing us to make sure that
     * events or RSVP sessions don't start before the day they were created.



     */
      function compareDates($seconsdsd)

    {

        $event = $this->data["Event"];
        $first = $event["event_start"];
        $dtz= new DateTimeZone("UTC");
        $start= new DateTime($first, $dtz);
        $s=$start->format('U');
        $second = $event["event_end"];
        $end= new DateTime($second, $dtz);
        $s2=$end->format('U');
           if ($s> $s2)
             {
              return false;

             }

             else

             {
              return true;

             }


    }/*This function is designed to check the event start and event end values against each other
     * in much the same way that the above function does for event start and today's date
     * It takes both values turns them into new date time objects, and then proceeds to
     * compare them.  If the event start date is greater than the event end date
     *  it will send back an error.

   

*/

      function RSVPcompareDates($seconsdsd)

    {

        $event = $this->data["Event"];
        $first = $event["rsvp_start"];
        $dtz= new DateTimeZone("UTC");
        $start= new DateTime($first, $dtz);
        $s=$start->format('U');
        $second = $event["rsvp_end"];
        $end= new DateTime($second, $dtz);
        $s2=$end->format('U');
           if ($s> $s2)
             {
              return false;

             }

             else

             {
              return true;

             }


    }/*This function is designed to check the RSVPstart and RSVPend values against each other
     * in much the same way that the above function does for event start and event end.
     * It takes both values turns them into new date time objects, and then proceeds to
     * compare them.  If the event start date is greater than the RSVP start date
     *  it will send back an error.



*/




    function RSVPDate($seconsdsd)

    {

        $event = $this->data["Event"];
        $first = $event["rsvp_start"];
        $dtz= new DateTimeZone("UTC");
        $start= new DateTime($first, $dtz);
        $s=$start->format('U');
        $second = $event["event_start"];
        $end= new DateTime($second, $dtz);
        $s2=$end->format('U');
           if ($s> $s2)
             {
              return false;

             }

             else

             {
              return true;

             }


    }/*This function is designed to check the RSVPstart and RSVP end values against each other
     * in much the same way that the above function does for event start and event end.
     * It takes both values turns them into new date time objects, and then proceeds to
     * compare them.  If the event start date is greater than the RSVP start date
     *  it will send back an error.




*/



}

?>